﻿using System;
using System.Data;

namespace DAL
{
    public static class DalOrder_Sell
    {
        private static readonly clsConnect Cls = new clsConnect();
        #region[Get-Data]
        public static DataTable GetOrderSell(string keywords)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@KEYWORDS", keywords);
            return Cls.GetData("sp_tb_Order_Sell_Get");
        }
        public static DataTable GetOrderSellEdit(int id)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Sell_Id", id);
            return Cls.GetData("sp_tb_Order_Sell_Get_Edit");
        }
        public static DataTable GetOrderSellDetailEdit(int id)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Sell_Id", id);
            return Cls.GetData("sp_tb_Order_Sell_Detail_Get_Edit");
        }
        public static DataTable GetOrderMaterial(string keywords)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@KEYWORDS", keywords);
            return Cls.GetData("sp_Material_Get_Sell_New");
        }
        public static DataTable GetAutoID(string colunmName, string tableName)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@ColunmName", colunmName);
            Cls.AddParameter("@TableName", tableName);
            return Cls.GetData("sp_Create_Code");
        }
        public static DataTable GetOrderSellDetailExists()
        {
            Cls.CreateNewSqlCommand();
            return Cls.GetData("sp_Order_Sell_Detail_Check_Exists");
        }
        public static DataTable GetOrderSellDetailTemp(int userId)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Users_Id", userId);
            return Cls.GetData("sp_tb_Order_Sell_Detail_Get_Temp");
        }
        public static DataTable GetOrderSellDetailA(int userId, int orderSellId)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Users_Id", userId);
            Cls.AddParameter("@Order_Sell_Id", orderSellId);
            return Cls.GetData("sp_tb_Order_Sell_Detail_Get_A");
        }
        public static DataTable GetOrderSellDetailSum(int userId, int flag)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Users_Id", userId);
            Cls.AddParameter("@Flag", flag);
            return Cls.GetData("sp_Order_Sell_Detail_Sum");
        }
        public static DataTable GetOrderSellDetailSumEdit(int id, int flag)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Sell_Id", id);
            Cls.AddParameter("@Flag", flag);
            return Cls.GetData("sp_Order_Sell_Detail_Sum_Edit");
        }
        public static DataTable GetOrderSellIsActive(int isActive)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@IsActive", isActive);
            return Cls.GetData("sp_tb_Order_Sell_Fillter_IsActive");
        }
        public static DataTable GetOrderSellFillterDate(string dateBegin,string dateEnd)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@DateBegin", dateBegin);
            Cls.AddParameter("@EndBegin", dateEnd);
            return Cls.GetData("sp_tb_Order_Sell_FillterDate");
        }
        public static DataTable GetOrderSellDetailSumExport(int id, int flag)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Sell_Id", id);
            Cls.AddParameter("@Flag", flag);
            return Cls.GetData("sp_Order_Sell_Detail_Sum_Edit_Export");
        }
        #endregion

        #region[Insert-Update-Delete]
        public static bool InsertOrderSell(EtOrderSell obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Sell_Code", obj.Order_Sell_Code);
            Cls.AddParameter("@Order_Sell_DateBegin", obj.Order_Sell_DateBegin);
            Cls.AddParameter("@Order_Sell_Tax_Code", obj.Order_Sell_Tax_Code);
            Cls.AddParameter("@Order_Sell_Description", obj.Order_Sell_Description);
            Cls.AddParameter("@Order_Sell_Payment_Type", obj.Order_Sell_Payment_Type);
            Cls.AddParameter("@Customer_ID", obj.Customer_ID);
            Cls.AddParameter("@IsActive", obj.IsActive);
            Cls.AddParameter("@Users_Id", obj.Users_Id);
            Cls.AddParameter("@Note", obj.Note);
            Cls.AddParameter("@TypeSell", obj.TypeSell);
            obj.Msg = Cls.ExecuteNonQueryOutput("sp_tb_Order_Sell_Insert", "@Msg");
            return true;
        }
        public static bool InsertOrderSellDetail(EtOrderSellDetail obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Sell_Id", obj.Order_Sell_Id);
            Cls.AddParameter("@Materials_Id", obj.Materials_Id);
            Cls.AddParameter("@Order_Detail_Quality", obj.Order_Detail_Quality);
            Cls.AddParameter("@Order_Detail_Price", obj.Order_Detail_Price);
            Cls.AddParameter("@Order_Detail_CK_Percent", obj.Order_Detail_CK_Percent);
            Cls.AddParameter("@Order_Detail_CK", obj.Order_Detail_CK);
            Cls.AddParameter("@Order_Detail_VAT_Percent", obj.Order_Detail_VAT_Percent);
            Cls.AddParameter("@Order_Detail_VAT", obj.Order_Detail_VAT);
            Cls.AddParameter("@Order_Detail_Total_Price", obj.Order_Detail_Total_Price);
            Cls.AddParameter("@Flag", obj.Flag);
            Cls.AddParameter("@Users_ID", obj.Users_Id);
            Cls.AddParameter("@Order_Detail_Quality_Remain", obj.Order_Detail_Quality_Remain);
            obj.Msg = Cls.ExecuteNonQueryOutput("sp_tb_Order_Sell_Detail_Insert", "@Msg");
            return true;
        }
        public static bool UpdateQuanlityOrder(EtOrderSellDetail obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Materials_ID", obj.Materials_Id);
            Cls.AddParameter("@Order_Sell_Detail_Id", obj.Order_Sell_Detail_Id);
            Cls.AddParameter("@Order_Detail_Quality", obj.Order_Detail_Quality);
            Cls.AddParameter("@Order_Detail_Price", obj.Order_Detail_Price);
            Cls.AddParameter("@Order_Detail_CK_Percent", obj.Order_Detail_CK_Percent);
            Cls.AddParameter("@Order_Detail_VAT_Percent", obj.Order_Detail_VAT_Percent);
            Cls.AddParameter("@Order_Detail_Quality_Remain", obj.Order_Detail_Quality_Remain);
            Cls.AddParameter("@Flag", obj.Flag);
            Cls.AddParameter("@Users_Id", obj.Users_Id);
            obj.Msg = Cls.ExecuteNonQueryOutput("sp_tb_Order_Sell_Detail_Update_Quanlity", "@Msg");
            return true;
        }
        public static bool UpdateQuanlityOrderRoot(EtOrderSellDetail obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Materials_ID", obj.Materials_Id);
            Cls.AddParameter("@Order_Sell_Detail_Id", obj.Order_Sell_Detail_Id);
            Cls.AddParameter("@Order_Detail_Quality", obj.Order_Detail_Quality);
            Cls.AddParameter("@Order_Detail_CK_Percent", obj.Order_Detail_CK_Percent);
            Cls.AddParameter("@Order_Detail_VAT_Percent", obj.Order_Detail_VAT_Percent);
            Cls.AddParameter("@Order_Detail_Quality_Remain", obj.Order_Detail_Quality_Remain);
            Cls.AddParameter("@Flag", obj.Flag);
            Cls.AddParameter("@Users_Id", obj.Users_Id);
            obj.Msg = Cls.ExecuteNonQueryOutput("sp_tb_Order_Sell_Detail_Update_Quanlity_Root", "@Msg");
            return true;
        }
        public static bool UpdateOrderSell(EtOrderSell obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Sell_Id", obj.Order_Sell_Id);
            Cls.AddParameter("@Order_Sell_Tax_Code", obj.Order_Sell_Tax_Code);
            Cls.AddParameter("@Order_Sell_Description", obj.Order_Sell_Description);
            Cls.AddParameter("@Users_Id", obj.Users_Id);
            Cls.AddParameter("@Note", obj.Note);
            return Cls.ExecuteNonQuery("sp_tb_Order_Sell_Update");
        }
        public static bool DeleteSellOrderDetail(EtOrderSellDetail obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Users_Id", obj.Users_Id);
            return Cls.ExecuteNonQuery("sp_Order_Sell_Detail_Del");
        }
        public static bool DeleteSellOrder(EtOrderSell obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Sell_Id", obj.Order_Sell_Id);
            obj.Msg = Cls.ExecuteNonQueryOutput("sp_Order_Sell_Delete", "@Msg");
            return true;
        }
        public static bool DeleteSellDetailMaterial(EtOrderSellDetail obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Sell_Detail_Id", obj.Order_Sell_Detail_Id);
            return Cls.ExecuteNonQuery("sp_Order_Sell_Detail_DelMaterial");
        }
        public static bool Review(EtOrderSell obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Sell_Id", obj.Order_Sell_Id);
            Cls.AddParameter("@Users_Id_Review", obj.Users_Id_Review);
            Cls.AddParameter("@IsActive", obj.IsActive);
            obj.Msg = Cls.ExecuteNonQueryOutput("sp_Order_Sell_Review", "@Msg");
            return true;
        }
        public static bool ReviewMaterial(EtOrderSellDetail obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Sell_Id", obj.Order_Sell_Id);
            Cls.AddParameter("@Order_Sell_Detail_Id", obj.Order_Sell_Detail_Id);
            Cls.AddParameter("@IsActive", obj.IsActive);
            obj.Msg = Cls.ExecuteNonQueryOutput("sp_Order_Sell_ReviewMaterial", "@Msg");
            return true;
        }
        public static bool UnReviewMaterial(EtOrderSellDetail obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Sell_Id", obj.Order_Sell_Id);
            Cls.AddParameter("@Order_Sell_Detail_Id", obj.Order_Sell_Detail_Id);
            Cls.AddParameter("@IsActive", obj.IsActive);
            obj.Msg = Cls.ExecuteNonQueryOutput("sp_Order_Sell_UnReviewMaterial", "@Msg");
            return true;
        }
         public static bool UnReView(EtOrderSell obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Sell_Id", obj.Order_Sell_Id);
            Cls.AddParameter("@Users_Id_Review", obj.Users_Id_Review);
            Cls.AddParameter("@IsActive", obj.IsActive);
            obj.Msg = Cls.ExecuteNonQueryOutput("sp_Order_Sell_UnReview", "@Msg");
            return true;
        }
        #endregion
    }

    public class EtOrderSell
    {
        public int ?  Order_Sell_Id { get; set; }
        public string Order_Sell_Code { get; set; }
        public DateTime Order_Sell_DateBegin { get; set; }
        public DateTime Order_Sell_DateEnd { get; set; }
        public string Order_Sell_Tax_Code { get; set; }
        public string Order_Sell_Description { get; set; }
        public string Order_Sell_Payment_Type { get; set; }
        public int ? Order_Sell_Uesr_ID { get; set; }
        public int ? Customer_ID { get; set; }
        public bool IsActive { get; set; }
        public int ? Users_Id { get; set; }
        public int ? Users_Id_Review { get; set; }
        public DateTime DateTimeReview { get; set; }
        public int Msg { get; set; }
        public string Note { get; set; }
        public string TypeSell { get; set; }
    }
    public class EtOrderSellDetail
    {
        public int Order_Sell_Detail_Id { get; set; }
        public int Order_Sell_Id { get; set; }
        public int Materials_Id { get; set; }
        public int Order_Detail_Quality { get; set; }
        public float Order_Detail_Price { get; set; }
        public float Order_Detail_CK_Percent { get; set; }
        public float Order_Detail_CK { get; set; }
        public float Order_Detail_VAT_Percent { get; set; }
        public float Order_Detail_VAT { get; set; }
        public float Order_Detail_Total_Price { get; set; }
        public int Flag { get; set; }
        public int Users_Id { get; set; }
        public int Order_Detail_Quality_Remain { get; set; }
        public int Msg { get; set; }
        public bool IsActive { get; set; }
    }
}
